import requests
import time
from openpyxl import load_workbook, Workbook
import jieba
import random
from threading import Thread

headers = [
    {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0",
        'Cookie': 'UOR=cn.bing.com,www.weibo.com,cn.bing.com; SINAGLOBAL=3376875829730.0796.1703321410567; SCF=AiA_HYJTlLkBCcsMsl-oKxsMMa2O9SJFLbj9A8nw2yEcsN39cwAOyK0lllgKkAfH7n99HZy_C-nD5UenoPxiaAA.; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5PnbzGzqFMUYIPIn4m9JFn5JpX5KMhUgL.FoqpeoqNS0qceh.2dJLoIpRLxK.LB.zL1hBLxK-LB-BL1K5LxK-L12BL12-cSoqt; ULV=1708233151395:8:3:1:8378107300570.1455.1708233151353:1708142811067; ALF=1710996305; SUB=_2A25I0F4BDeRhGeBP6VQW9yjKyzWIHXVrrN_JrDV8PUJbkNB-LUjmkW1NRXLTWCwYHjyi9uzhwOKivdrxNC81z0Bx; XSRF-TOKEN=SyJqkaMRhl7b8lAngW0hWyxV; WBPSESS=SdNjAm69kt0J8TnnXl_t41OMkI5ebP3T_X4Bw9r1RMMhBrMGv5Nap3IJiLzI52ycGLgZl_EAGHBMpA2sniX5wyctuKHrTsYCs5mgMqt9T6GAK5p41WexVQkcKc3z5TaIkQgE-4s4POweb-lTdZwRBA==',
        'X-Xsrf-Token': 'B35RKsg-pmLglbVwwePUNt2x'

    },
    {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
        'Cookie': 'XSRF-TOKEN=SNlkkGgyuD8PQFDlhr8Rpc1g; PC_TOKEN=3fdc542f75; login_sid_t=f6713eed5799de1b3bde7343898ffde4; cross_origin_proto=SSL; WBStorage=267ec170|undefined; _s_tentry=weibo.com; Apache=9938386310328.768.1708512298424; SINAGLOBAL=9938386310328.768.1708512298424; ULV=1708512298427:1:1:1:9938386310328.768.1708512298424:; wb_view_log=1600*10002; appkey=; geetest_token=c5d72ae1b685cc2035cb9b47c7257c8a; ALF=1711104406; SUB=_2A25I0aTGDeRhGeFH61cV8C7KzzyIHXVrrrgOrDV8PUJbkNANLXn8kW1NeD6w2h2UDBF1QMXeyY1koiEeiq6QS_ed; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhhpN32T1PWBfMTGo9WI8Zv5JpX5KzhUgL.FoM4eh-Xeh5cSh52dJLoIp7LxKML1KBLBKnLxKqL1hnLBoMRSK2ESh5fehef; WBPSESS=F-g56V5WRtN66TC4al6V9l0fEwcgr8I3fbpJmrN7RYsvq1ryWek53-Dg29OD-aRca7aWCmKDEp8t2zO0BGVRDcEYSBn3CFZyVvdRgAK2lIiRWlWLBHHs8Q_E0O-9Os5snxnsXKRAYSd0TExB_hGu1A==',
    }]

wb = load_workbook('微博17.xlsx')
sheet = wb.active
max = sheet.max_row

PROXY_POOL_URL = 'http://api.yilian.top/v2/proxy/proxies?token=qBKMYIz6exIYrPUx9bwdy6ovUlmphrWu&pull_num=70&format=json&protocol=1&separator=1&auto_shield=true'


def get_proxy():
    try:
        response = requests.get(PROXY_POOL_URL)
        proxy_list = []
        response.raise_for_status()
        proxy_data = response.json()
        for i in proxy_data['data']:
            proxy_data = str(i['proxy_user']) + ':' + str(i['proxy_pass']) + '@' + str(
                i['ip']) + ':' + str(i['proxy_port']).strip()
            proxy_list.append(proxy_data)
        proxy = random.choice(proxy_list)
        return proxy
    except requests.RequestException as e:
        print(f'Error getting proxy: {e}')
        return None


def get(uid, id, sheets, ws):
    sign = 1
    while True:
        try:
            proxy = 'http://' + get_proxy()
            print(proxy)
            proxies = {
                'http': proxy,
                'https': proxy
            }
            if sign == 1:
                sign += 1
                url = f'https://weibo.com/ajax/statuses/buildComments?is_reload=1&id={id}&is_show_bulletin=2&is_mix=0&count=10&uid={uid}&fetch_level=0&locale=zh-CN'
            else:
                url = f'https://weibo.com/ajax/statuses/buildComments?flow=0&is_reload=1&id={id}&is_show_bulletin=2&is_mix=0&max_id={max_id}&count=20&uid=1784473157&fetch_level=0&locale=zh-C'
            try:
                response = requests.get(url, headers=random.choice(headers), proxies=proxies).json()

                time.sleep(random.randint(1, 2))
            except:
                break
            max_id = response['max_id']
            if max_id == 0:
                break
        except Exception as  e:
            continue

        # 一级评论
        try:
            for i in response['data']:
                first_level_comments_data = []
                try:
                    neirong = i['text']
                    data = [neirong]
                    print(data)
                    sheets.append(data)
                    ws.save(f'{uid}.xlsx')
                    first_level_comments_data.append(data)
                    pinglun_id = i['id']
                    n = 0
                    time.sleep(random.randint(0, 1))
                    time.sleep(random.randint(0, 1))
                    while True:
                        proxy = 'http://' + get_proxy()

                        proxies = {
                            'http': proxy,
                            'https': proxy
                        }
                        if n == 0:
                            n += 1
                            next_url = f'https://weibo.com/ajax/statuses/buildComments?is_reload=1&id={pinglun_id}&is_show_bulletin=2&is_mix=1&fetch_level=1&max_id=0&count=20&uid=1784473157&locale=zh-CN'
                        else:
                            next_url = f'https://weibo.com/ajax/statuses/buildComments?flow=0&is_reload=1&id={pinglun_id}&is_show_bulletin=2&is_mix=1&fetch_level=1&max_id={next_max_id}&count=20&uid=1784473157&locale=zh-CN'

                        res1 = requests.get(next_url, headers=random.choice(headers), proxies=proxies).json()
                        time.sleep(random.randint(0, 1))
                        next_max_id = res1['max_id']
                        if next_max_id == 0:
                            break
                    if res1['data']:
                        for s in res1['data']:
                            neirong1 = s['text_raw']

                            data1 = [neirong1]
                            print(data1)
                            sheets.append(data1)
                            ws.save(f'{uid}.xlsx')
                    else:
                        ws.save(f'{uid}.xlsx')

                except:
                    ws.save(f'{uid}.xlsx')
                    continue
        except:
            ws.save(f'{uid}.xlsx')
            pass


def start(user_id, uid):
    first = 0
    number = 0
    ws = Workbook()
    sheets = ws.active
    for every_blog in range(1, 4):
        if first == 0:
            res = requests.get(f'https://weibo.com/ajax/statuses/mymblog?uid={uid}&page=1&feature=0',
                               headers=random.choice(headers)).json()
            first += 1
        else:
            res = requests.get(
                f'https://weibo.com/ajax/statuses/mymblog?uid={uid}&page=2&feature=0&since_id={since_id}',
                headers=random.choice(headers)).json()
        since_id = res['data']['since_id']
        for id in res['data']['list']:
            print(id['mid'])
            number += 1
            get(uid, id['mid'], sheets, ws)
            if number >= 50:
                break

        print(number)


if __name__ == '__main__':
    threads = []
    sign = 1
    for dir in range(1, max+1):
        user_id = sheet[f'A{dir}'].value
        if sign == 1:
            print(user_id)
            result = jieba.lcut(user_id)[-8]
            uid = result
            print(uid)
            sign += 1
        elif 'u' in user_id:
            uid = jieba.lcut(user_id)[-1]
            print(uid)
        thread = Thread(target=start, args=(user_id, uid))
        threads.append(thread)
        thread.start()
    # 等待所有线程完成
    for thread in threads:
        thread.join()
